package com.anlu.concurrent.ch08b.service;
import com.anlu.concurrent.ch08b.assist.*;
import com.anlu.concurrent.ch08b.service.question.SingleQstService;
import com.anlu.concurrent.ch08b.vo.*;
import com.anlu.concurrent.ch08b.service.question.*;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.ExecutionException;

/**
 * 类说明：处理文档的服务，包括文档中题目的处理和文档生成后的上传
 */
public class ProduceDocService {
    /**
     * 上传文档到网络
     * @param docFileName 实际文档在本地的存储位置
     * @return 上传后的网络存储地址
     */
    public static String upLoadDoc(String docFileName){
        Random r=new Random();
        SL_Busi.buisness(9000+r.nextInt(400));
        return "http://www.xxxx.com/file/upload/"+docFileName;
    }

    /**
     *将待处理的文档处理为本地实际的PDF文档
     * @param pendingDocVo 待处理文档
     * @return 实际文档在本地的存储位置
     */
    public static String makeDoc(SrcDocVo pendingDocVo){
        System.out.println("开始处理文档："+ pendingDocVo.getDocName());
        StringBuffer sb = new StringBuffer();
        //循环处理文档中的每个题目
        for(Integer questionId: pendingDocVo.getQuestionList()){
            sb.append(SingleQstService.makeQuestion(questionId));
        }
        return "complete_"+System.currentTimeMillis()+"_"
                + pendingDocVo.getDocName()+".pdf";
    }

    //异步化处理题目的方法
    public static String makeDocAsyn(SrcDocVo pendingDocVo) throws
            InterruptedException, ExecutionException {
        System.out.println("开始处理文档："+ pendingDocVo.getDocName());

        Map<Integer,TaskResultVo> qstResultMap =new HashMap<>();
        //循环处理文档中的每个题目,准备并行化处理题目

        for (Integer questionid:pendingDocVo.getQuestionList()){
          qstResultMap.put(questionid, ParallerQstService.makeQuestion(questionid));
        }

        StringBuffer sb = new StringBuffer();
        for (Integer quesitonId:pendingDocVo.getQuestionList()){
            TaskResultVo result = qstResultMap.get(quesitonId);
            sb.append(result.getQuestionDetail()==null?result.getQuestionFuture().get().getQuestionDetail()
                    :result.getQuestionDetail());
        }
        return "complete_"+System.currentTimeMillis()+"_"
                + pendingDocVo.getDocName()+".pdf";
    }




}
